/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.action;

import com.darren.euc.entity.*;
import com.darren.euc.repository.MemberRepository;
import com.darren.euc.service.*;
import com.darren.euc.util.PropertyHelper;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.darren.euc.util.GenegateDomainUtil.isAvaliable;

/**
 * 线上运营管理
 *
 * @author fangwenhao
 * @created 2018/3/11
 */
@Controller
@RequestMapping("/onlineops")
public class OnlineOpsAction {

    @Autowired
    private MsgService msgService;

    @Autowired
    private SpaceService spaceService;

    @Autowired
    private SuggestService suggestService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private BannerService bannerService;

    @Autowired
    private ServiceTelService serviceTelService;

    @Autowired
    private ActivityMemberRelService activityMemberRelService;

    @Autowired
    private MemberRepository memberRepository;

    /**
     * 查询站内信
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/msg/query",method = RequestMethod.POST)
    public ResponseJSON queryMsg(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        PageBean pageBean = msgService.queryForPageMsgHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
    }

    /**
     * 查询站内信详情
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/msg/detail",method = RequestMethod.POST)
    public ResponseJSON detailMsg(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        Long id = requestJson.getJSONObject("data").getLong("id");
        Msg msg = msgService.findById(id);
        responseJson.setData(msg);
        return responseJson;
    }

    /**
     * 删除站内信详情
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/msg/delete",method = RequestMethod.POST)
    public ResponseJSON deleteMsg(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        Long id = requestJson.getJSONObject("data").getLong("id");
        msgService.delete(id);
        return responseJson;
    }


    /**
     * 新增站内信
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/msg/add",method = RequestMethod.POST)
    public ResponseJSON addMsg(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        String title = data.getString("title");
        String content = data.getString("content");
        User user = (User) request.getAttribute("user");
        long userAreaId = data.getLong("userAreaId");//用户区域
        int userType = data.getInt("userType");//用户类型 - 这个弃用
        int tagretType = data.getInt("targetType");//目标类型
        String tagretInfo = data.getString("targetInfo");//目标信息
        Msg msg = new Msg();
        msg.setTitle(title);
        msg.setContent(content);
        msg.setCreatePerson(user.getName());
        msg.setTagretType(tagretType);
        msg.setUserType(userType);// 这个弃用
        String targetUserType;
        if (tagretType == 1){
            targetUserType = "0"; // 表示全部
        }else if(tagretType == 2){
            targetUserType = tagretInfo.replace("|",",");
        }else if (tagretType == 3){
            targetUserType = "-1"; // 表示特定用户
            Member member = memberRepository.findByMobile(tagretInfo);
            if (member == null){
                return new ResponseJSON(-3,"查无此号",null);
            }
            if (userAreaId == 0 || member.getSpaceId().longValue() != userAreaId){
                return new ResponseJSON(-4,"空间与会员信息不一致",null);
            }
        }else {
            String[] idStrArr = tagretInfo.split("\\|");
            userAreaId = Long.valueOf(idStrArr[0]); // 此处表示空间id, 0表示全部
            tagretInfo = idStrArr[1]; // 此处表示客户id, 0表示全部
            targetUserType = "-2"; // 表示用户分组
        }
        msg.setUserAreaId(userAreaId);
        msg.setTagretInfo(tagretInfo);
        msg.setUserArea(targetUserType);
        msg.setAddTime(new Date());
        msg.setModTime(new Date());
        msg.setIsDeleted(0);
        msgService.save(msg);
        return responseJson;
    }

    /**
     * 查询投诉建议
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/suggest/query",method = RequestMethod.POST)
    public ResponseJSON querySuggest(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        PageBean pageBean = suggestService.queryForPageSuggestHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
    }

    /**
     * 查看投诉建议详情
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/suggest/detail",method = RequestMethod.POST)
    public ResponseJSON detailSuggest(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        Long id = requestJson.getJSONObject("data").getLong("id");
        Suggest suggest = suggestService.findById(id);
        responseJson.setData(suggest);
        return responseJson;
    }

    /**
     * 处理投诉建议
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/suggest/update",method = RequestMethod.POST)
    public ResponseJSON updateSuggest(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        Integer status = data.getInt("status");
        String result = data.getString("result");
        Suggest suggest = suggestService.findById(id);
        suggest.setResult(result);
        suggest.setStatus(status);
        suggestService.update(suggest);
        return responseJson;
    }

    /**
     * 查询公告
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/notice/query",method = RequestMethod.POST)
    public ResponseJSON queryNotice(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        PageBean pageBean = noticeService.queryForPageNoticeHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
    }

    /**
     * 查看公告详情
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/notice/detail",method = RequestMethod.POST)
    public ResponseJSON detailNotice(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        Notice notice = noticeService.findById(id);
        responseJson.setData(notice);
        return responseJson;
    }

    /**
     * 编辑公告
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/notice/aoe",method = RequestMethod.POST)
    public ResponseJSON editNotice(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        User user = (User) request.getAttribute("user");
        JSONObject data = requestJson.getJSONObject("data");
        data.put("personName",user.getName());
        Notice notice = noticeService.editById(data);
        if (notice == null|| !isAvaliable()){
            return new ResponseJSON(-3,"操作失败，请校验数据",null);
        }
        responseJson.setData(notice);
        return responseJson;
    }

    /**
     * 删除公告
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/notice/delete",method = RequestMethod.POST)
    public ResponseJSON deleteNotice(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        Notice notice = noticeService.deleteById(id);
        responseJson.setData(notice);
        return responseJson;
    }

    /**
     * 查询活动
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/activity/query",method = RequestMethod.POST)
    public ResponseJSON queryActivity(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        PageBean pageBean = activityService.queryForPageActivityHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
    }


    /**
     * 新建或编辑活动
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/activity/aoe",method = RequestMethod.POST)
    public ResponseJSON aoeActivity(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        User user = (User) request.getAttribute("user");
        JSONObject data = requestJson.getJSONObject("data");
        data.put("personName",user.getName());
        String userArea = data.getString("userArea");
        if (userArea.equals("请选择")|| !isAvaliable()){
            return new ResponseJSON(-1,"请选择可用的空间",null);
        }
        Activity activity = activityService.aoe(data);
        if (activity == null){
            return new ResponseJSON(-3,"操作失败,请校验数据",null);
        }
        responseJson.setData(activity);
        return responseJson;
    }

    /**
     * 查看活动详情
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/activity/detail",method = RequestMethod.POST)
    public ResponseJSON detailActivity(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        Activity activity = activityService.findById(id);
        responseJson.setData(activity);
        return responseJson;
    }


    /**
     * 删除活动
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/activity/delete",method = RequestMethod.POST)
    public ResponseJSON deleteActivity(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        Activity activity = activityService.delete(id);
        responseJson.setData(activity);
        return responseJson;
    }


    /**
     * 查询banner
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/banner/query",method = RequestMethod.POST)
    public ResponseJSON queryBanner(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        PageBean pageBean = bannerService.queryForPageBannerHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
    }

    /**
     * 编辑或新增
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/banner/aoe",method = RequestMethod.POST)
    public ResponseJSON aoeBanner(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        User user = (User) request.getAttribute("user");
        JSONObject data = requestJson.getJSONObject("data");
        data.put("personName",user.getName());
        Banner banner = bannerService.aoe(data);
        responseJson.setData(banner);
        return responseJson;
    }

    /**
     * 删除banner
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/banner/delete",method = RequestMethod.POST)
    public ResponseJSON deleteBanner(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        Banner banner = bannerService.delete(id);
        responseJson.setData(banner);
        return responseJson;
    }


    /**
     * 查看热线
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/tel/detail",method = RequestMethod.POST)
    public ResponseJSON detailTel(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        Long id = data.getLong("id");
        ServiceTel serviceTel = serviceTelService.findById(id);
        responseJson.setData(serviceTel);
        return responseJson;
    }

    /**
     * 编辑热线
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/tel/edit",method = RequestMethod.POST)
    public ResponseJSON editlTel(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        ServiceTel serviceTel = serviceTelService.update(data);
        responseJson.setData(serviceTel);
        return responseJson;
    }

    /**
     * 查询报名列表
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/actimem/query",method = RequestMethod.POST)
    public ResponseJSON queryActimem(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        PageBean pageBean = activityMemberRelService.queryForPageActivityMemberRelHQL(data,PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
    }

    /**
     * 签到
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/actimem/check",method = RequestMethod.POST)
    public ResponseJSON check(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        JSONObject data = requestJson.getJSONObject("data");
        ActivityMemberRel activityMemberRel = activityMemberRelService.updateCheck(data.getLong("id"));
        responseJson.setData(activityMemberRel);
        return responseJson;
    }


}
